//
//  ABNavigationBar.swift
//  ABNavigation
//
//  Created by FaiWong on 2023/2/26.
//

import UIKit
import ABExtension
public class ABNavigationBarConfig: NSObject {
    public var title:String = ""
    public var titleFont:UIFont? = UIFont.PingFangMedium_ab(size: 18.0)
    public var titleColor:UIColor? = UIColor(hexStr: "222222")
    public var whiteBackImage:UIImage?
    public var blackBackImage:UIImage?
}


public class ABNavigationBar: UIView {

    var contentView:UIView!
    var titleLabel:UILabel!
    var rightButton:UIButton!
    var backButton:UIButton!
    var conf:ABNavigationBarConfig = ABNavigationBarConfig()
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView = UIView(frame: CGRect(x: 0, y: kStatusBarHeight, width: self.width_ab, height: 44.0))
        self.addSubview(self.contentView)
        
        
        titleLabel = UILabel(frame: .zero)
        self.contentView.addSubview(titleLabel)
        
        self.backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
       
        self.contentView.addSubview(self.backButton)
        self.backButton.addTarget(self, action: #selector(onBack), for: .touchUpInside)
        
//        self.rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
//        self.rightButton.setImage(UIImage(named: "icon_setting_white"), for: .normal)
//        self.rightButton.backgroundColor = .black.withAlphaComponent(0.5)
//        self.rightButton.layer.cornerRadius = 22.0
//        self.contentView.addSubview(self.rightButton)
        
        refreshToWhiteStyle()
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.titleLabel.centerX_ab = self.width_ab/2.0
        self.titleLabel.centerY_ab = self.contentView.height_ab/2.0
        
        self.backButton.left_ab = 16.0
        self.backButton.centerY_ab = self.contentView.height_ab/2
        
//        self.rightButton.left = self.width-16.0-self.rightButton.width
//        self.rightButton.centerY = self.contentView.height/2
        
    }
    
    public func setData(conf:ABNavigationBarConfig) {
        self.titleLabel.text = conf.title
        self.titleLabel.sizeToFit()
        titleLabel.font = conf.titleFont
        self.titleLabel.textColor = conf.titleColor
        
    }
    
    @objc public func onBack() {
        UIApplication.getTopViewController()?.navigationController?.popViewController(animated: true)
    }
    
    
    public func refreshToWhiteStyle() {
        self.backButton.setImage(self.conf.whiteBackImage, for: .normal)
    }
    
    public func refreshToBlackStyle() {
        self.backButton.setImage(self.conf.whiteBackImage, for: .normal)
    }
}
